package org.csu.cms.controller;

import com.alibaba.fastjson2.JSON;
import lombok.Data;
import org.csu.cms.domain.Order;
import org.csu.cms.service.OrderFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class OrderFormController {


    @Data
    private class Result {
        String result;
    }

    @Autowired
    private OrderFormService orderFormService;


    @PostMapping("/orderform/neworder")
    @ResponseBody
    public String addNewOrder(@RequestBody Order order) {//新增订单
        System.out.println(order);
        if (orderFormService.addOrder(order)) {
            Result str = new Result();
            str.result = "success";
            return JSON.toJSONString(str);
        } else {
            Result str = new Result();
            str.result = "failure";
            return JSON.toJSONString(str);
        }
    }

    @GetMapping("/orderform")
    @ResponseBody
    public String getOrderForm() {//获取订单
        return JSON.toJSONString(orderFormService.getOrderForm());
    }


    @GetMapping("/orderform/{ordernumber}")
    @ResponseBody
    public String getDetailedOrder(@PathVariable(name = "ordernumber") String orderNumber) {//打开订单详情页
        return JSON.toJSONString(orderFormService.getDetailedOrder(orderNumber));
    }

    @GetMapping("/orderform/search/{context}")
    @ResponseBody
    public String searchOrder(@PathVariable(name = "context") String context) {//搜索订单

        System.out.println(context);
        return JSON.toJSONString(orderFormService.Search(context));

    }

    @PutMapping("/orderform/updates/{ordernumber}")
    @ResponseBody
    public String updateOrder(@RequestBody Order order) {//更新订单
//        JSONObject jsonObject=JSON.parseObject(json);
//        System.out.println(jsonObject);
//        Order order=new Order();
//        order.setOrderNumber(jsonObject.getObject("orderNumber",String.class));
        if (orderFormService.updateOrder(order)) {
            Result str = new Result();
            str.result = "success";
            return JSON.toJSONString(str);
        } else {
            Result str = new Result();
            str.result = "failure";
            return JSON.toJSONString(str);
        }


    }


    @DeleteMapping("/orderForm/deletions/{ordernumber}")
    @ResponseBody
    public String deleteOrder(@PathVariable(name = "ordernumber") String orderNumber) {//删除订单
        if (orderFormService.deleteOrder(orderNumber)) {
            Result str = new Result();
            str.result = "success";
            return JSON.toJSONString(str);
        } else {
            Result str = new Result();
            str.result = "failure";
            return JSON.toJSONString(str);
        }

    }


    @PutMapping("/orderForm/send/{ordernumber}")
    @ResponseBody
    public String sendGoods(@PathVariable(name = "ordernumber") String orderNumber) {//送货
        if (orderFormService.sendGoods(orderNumber)) {
            Result str = new Result();
            str.result = "success";
            return JSON.toJSONString(str);
        } else {
            Result str = new Result();
            str.result = "failure";
            return JSON.toJSONString(str);
        }
    }

}
